package com.ruoyi.dynamic.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.dynamic.mapper.RoomRecordMapper;
import com.ruoyi.dynamic.model.constant.DynamicConstant;
import com.ruoyi.dynamic.model.pojo.entity.RoomRecord;
import com.ruoyi.dynamic.service.IRoomRecordService;
import com.ruoyi.system.api.RemoteAppUserService;
import com.ruoyi.system.api.model.user.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.smartcardio.CommandAPDU;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * <p>
 * 房间进入记录表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2024-11-27
 */
@Service
public class RoomRecordServiceImpl extends ServiceImpl<RoomRecordMapper, RoomRecord> implements IRoomRecordService {
    @Autowired
    private RemoteAppUserService remoteAppUserService;

    @Async
    @Override
    public void add(Long userId, Long roomId) {
        RoomRecord roomRecord = lambdaQuery()
                .eq(RoomRecord::getUserId, userId)
                .eq(RoomRecord::getRoomId, roomId).one();
        if (roomRecord == null) {
            R<AppUser> userInfo = remoteAppUserService.getUserInfo(userId, SecurityConstants.APP);
            AppUser data = userInfo.getData();
            roomRecord = new RoomRecord();
            roomRecord.setUserName(data.getNickName());
            roomRecord.setUserPhoto(data.getPhoto());
        }
        roomRecord.setCreateTime(new Date());
        roomRecord.setUserId(userId);
        roomRecord.setRoomId(roomId);
        roomRecord.setWatchTime(0);
        roomRecord.setQuitTime(new Date());
        roomRecord.setIsOnLine(DynamicConstant.RoomStatus.NO);
        saveOrUpdate(roomRecord);
    }

    @Async
    @Override
    public void leave(Long userId, Long roomId) {
        lambdaUpdate().eq(RoomRecord::getUserId, userId)
                .eq(RoomRecord::getRoomId, roomId)
                .set(RoomRecord::getIsOnLine, DynamicConstant.CheckStatus.YES)
                .set(RoomRecord::getQuitTime, new Date()).update();
    }

    @Override
    public Long roomUserTotalCount(Long id) {
        return lambdaQuery()
                .eq(RoomRecord::getRoomId, id).count();
    }

    @Override
    public Long roomUserLineCount(Long id) {
        return lambdaQuery()
                .eq(RoomRecord::getIsOnLine, DynamicConstant.CheckStatus.NO)
                .eq(RoomRecord::getRoomId, id).count();
    }

    @Override
    public String getMaxLook(Long id) {
        RoomRecord one = lambdaQuery()
                .eq(RoomRecord::getRoomId, id)
                .orderByDesc(RoomRecord::getWatchTime)
                .last("limit 1").one();
        if (CommonUtils.isNull(one)) return "";
        return DateUtils.convertSecondsToTime(one.getWatchTime());
    }

    @Override
    public Integer attentionCount(Long id) {
        return lambdaQuery()
                .eq(RoomRecord::getIsAttention, DynamicConstant.CheckStatus.YES)
                .eq(RoomRecord::getRoomId, id).list().stream().map(RoomRecord::getUserId).collect(Collectors.toSet()).size();
    }
}
